const { Icon } = require("../../../../models");
const { Op } = require("sequelize");
const iconGroupService = require("../../iconGroup/service/GroupServiceImpl");
module.exports = {
  // 保存分组信息
  async saveIcon(req, res, next) {
    try {
      const { iconGroupId } = req.body;

      // 校验图标分组是否存在
      await iconGroupService.checkIconGroupItem({
        body: { id: iconGroupId },
      });

      const iconItem = await Icon.create(req.body);
      if (!iconItem) throw new Error("保存失败");
      return res.cc(null, "保存成功");
    } catch (error) {
      next(error);
    }
  },

  // 删除图标
  async deleteIcon(req, res, next) {
    try {
      const { ids } = req.body;
      if (!ids || ids?.length === 0) throw new Error("图标集合ids不能为空");
      console.log(Array.isArray(ids));
      const iconFlag = await Icon.destroy({
        where: {
          id: {
            [Op.in]: ids,
          },
        },
      });
      if (!iconFlag) throw new Error("图标删除失败");
      return res.cc(null, "删除成功");
    } catch (error) {
      next(error);
    }
  },
};
